package io.geekidea.boot.shop.controller.web;

import io.geekidea.boot.auth.annotation.Permission;
import io.geekidea.boot.common.enums.SysLogType;
import io.geekidea.boot.framework.annotation.Log;
import io.geekidea.boot.framework.page.Paging;
import io.geekidea.boot.framework.response.ApiResult;
import io.geekidea.boot.shop.dto.CollectDto;
import io.geekidea.boot.shop.query.web.CollectQuery;
import io.geekidea.boot.shop.service.base.api.CollectService;
import io.geekidea.boot.shop.vo.web.CollectVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 收藏 控制器
 *
 * @author geekidea
 * @since 2024-10-27
 */
@Slf4j
@RestController
@Tag(name = "收藏")
@RequestMapping("/admin/collect")
public class CollectController {

    @Autowired
    private CollectService collectService;

    /**
     * 添加收藏
     *
     * @param dto
     * @return
     * @throws Exception
     */
    @Log(type = SysLogType.ADD)
    @Operation(summary = "添加收藏")
    @PostMapping("/addCollect")
    @Permission("collect:add")
    public ApiResult addCollect(@Valid @RequestBody CollectDto dto) {
        log.info("添加收藏：{}", dto);
        boolean flag = collectService.addCollect(dto);
        return ApiResult.result(flag);
    }

    /**
     * 修改收藏
     *
     * @param dto
     * @return
     * @throws Exception
     */
    @Log(type = SysLogType.UPDATE)
    @Operation(summary = "修改收藏")
    @PostMapping("/updateCollect")
    @Permission("collect:update")
    public ApiResult updateCollect(@Valid @RequestBody CollectDto dto) {
        log.info("修改收藏：{}", dto);
        boolean flag = collectService.updateCollect(dto);
        return ApiResult.result(flag);
    }

    /**
     * 删除收藏
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Log(type = SysLogType.DELETE)
    @Operation(summary = "删除收藏")
    @PostMapping("/deleteCollect/{id}")
    @Permission("collect:delete")
    public ApiResult deleteCollect(@PathVariable Integer id) {
        log.info("删除收藏：{}", id);
        boolean flag = collectService.deleteCollect(id);
        return ApiResult.result(flag);
    }

    /**
     * 获取收藏详情
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Operation(summary = "获取收藏详情")
    @PostMapping("/getCollect/{id}")
    @Permission("collect:info")
    public ApiResult<CollectVo> getCollect(@PathVariable Integer id) {
        log.info("获取收藏详情：{}", id);
        CollectVo collectVo = collectService.getCollectById(id);
        return ApiResult.success(collectVo);
    }

    /**
     * 获取收藏分页列表
     *
     * @param query
     * @return
     * @throws Exception
     */
    @Operation(summary = "获取收藏分页列表")
    @PostMapping("/getCollectPage")
    @Permission("collect:page")
    public ApiResult<CollectVo> getCollectPage(@Valid @RequestBody CollectQuery query) {
        log.info("获取收藏分页列表：{}", query);
        Paging<CollectVo> paging = collectService.getCollectPage(query);
        return ApiResult.success(paging);
    }

}
